/**
 * Program  : ProductListViewAdapter.java
 * Author   : qianj
 * Create   : 2012-4-5 下午02:49:04
 *
 * Copyright 2012 by newyulong Technologies Ltd.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information
 * of newyulong Technologies Ltd.("Confidential Information").  
 * You shall not disclose such Confidential Information and shall 
 * use it only in accordance with the terms of the license agreement 
 * you entered into with newyulong Technologies Ltd.
 *
 */

package com.newyulong.implatform.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.newyulong.implatform.R;
import com.newyulong.implatform.activity.VasDetailActivity;
import com.newyulong.implatform.bean.ProductJsonBean;
import com.newyulong.implatform.util.AsyncImageLoader;
import com.newyulong.implatform.util.AsyncImageLoader.ImageCallback;

/**
 * 产品列表listview适配器
 * @author qianj
 * @version 1.0.0
 * @2012-4-5 下午02:49:04
 */
public class VasListViewAdapter extends BaseAdapter {

	//private final String TAG = "ProductListViewAdapter";
	
	private Context context;
	ArrayList<ProductJsonBean> listViewRes;
	private AsyncImageLoader asyncImageLoader = null;
	
	public VasListViewAdapter(Context context,
			ArrayList<ProductJsonBean> listViewRes) {
		this.context = context;
		this.listViewRes = listViewRes;
		asyncImageLoader = new AsyncImageLoader();
	}
	
	//public int count = Defines.PAGE_SIZE;
	
	@Override
	public int getCount() {
		return listViewRes.size();
	}

	@Override
	public Object getItem(int position) {
		return listViewRes.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		
		final ProductListViewHolder holder;
		
		if (convertView == null) {
			holder = new ProductListViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.index_item_listview, null);
			holder.imageViewIcon = (ImageView) convertView
					.findViewById(R.id.image_item_icon);
			holder.productName = (TextView) convertView
					.findViewById(R.id.text_item_title);
			holder.productCount = (TextView) convertView
					.findViewById(R.id.text_item_count);
			holder.productPrice = (TextView) convertView
					.findViewById(R.id.text_item_price);
			holder.productPriceUnit = (TextView) convertView
					.findViewById(R.id.tv_price_unit);
			holder.imgBtnOpen = (Button) convertView
					.findViewById(R.id.image_btn_open);
			convertView.setTag(holder);
		} else {
			holder = (ProductListViewHolder) convertView.getTag();
		}
		
		// 设置holder的值 根据position
		//设置图标
		holder.imageViewIcon.setImageBitmap(null);
		Drawable drawable = asyncImageLoader.loadDrawable(
				listViewRes.get(position).getImgUrl(),
				new ImageCallback() {
					public void imageLoaded(Drawable imageDrawable,String imageUrl) {
						holder.imageViewIcon.setImageDrawable(imageDrawable);
					}
				});
		if (drawable == null) {
			holder.imageViewIcon.setImageResource(R.drawable.ic_launcher);
		} else {
			holder.imageViewIcon.setImageDrawable(drawable);
		}
		
		//设置产品名称
		String productName = listViewRes.get(position).getName();
		holder.productName.setText(productName);
		//价格(为了兼容浙江和福建的情况)
		String price = listViewRes.get(position).getPrice();
		if(price.contains(";")){
			//格式：“50;元/月”
			String p = price.split(";")[0];//价格
			String u = price.split(";")[1];//单位
			holder.productPrice.setText(p);
			holder.productPriceUnit.setText(u);
		} else {
			holder.productPrice.setText(price);
		}
		//推荐次数
		StringBuffer sb = new StringBuffer();
		sb.append("已推荐");
		sb.append(listViewRes.get(position).getRecommendCount());
		sb.append("次");
		holder.productCount.setText(sb.toString());
		
		//设置开通按键的事件监听
		holder.imgBtnOpen.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ProductJsonBean pjb = listViewRes.get(position);
				Intent i = new Intent(context,
						VasDetailActivity.class);
				i.putExtra("product", pjb);
				context.startActivity(i);
			}
		});
		return convertView;
	}

}
